<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2019/1/27
 * Time: 1:15 PM
 */

namespace api\controllers;


use api\models\Article;
use api\models\Comment;
use api\models\Trash;
use common\models\meta\ArticleMetaLike;
use Yii;
use yii\web\Response;

class TrashController extends BaseController
{
    public $modelClass = 'api\models\Trash';


    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index']);

        return $actions;
    }

    public function actionIndex()
    {
        $model = Trash::find()->where([
            'status' => Article::ARTICLE_PUBLISHED,
            'type'   => Article::ARTICLE,
        ])->orderBy('created_at DESC')->all();

        return $model;
    }

    public function actionLike($id)
    {
        $aid   = Yii::$app->getRequest()->post("aid");
        $model = new ArticleMetaLike();
        $model->setLike($aid);

        return $model->getLikeCount($aid);
    }

    public function actionCreateComment()
    {
        $commentModel = new Comment();
        if ($commentModel->load(Yii::$app->getRequest()->post()) && $commentModel->save()) {
            if (Yii::$app->feehi->website_comment_need_verify) {
                $tips = Yii::t('frontend', 'Comment waiting for approved.');

                return $tips;
            }
            $commentModel->uid = Yii::$app->getUser()->id;

            return $commentModel;
        } else {
            $temp = $commentModel->getErrors();
            $str  = '';
            foreach ($temp as $v) {
                $str .= $v[0] . "<br>";
            }
            Yii::$app->response->statusCode = 400;

            return ["error" => $str];
        }
    }

    public function actionViewComment($article_id)
    {
        $commentModel = new Comment();
        $commentList  = $commentModel->getCommentByAid($article_id);

        return $commentList;
    }


}